﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using THS.HtmlHelpers;
using Veteran_cms.Models;

namespace Veteran_cms.Controllers
{
    public class ManageTagController : BaseController
    {
        readonly Veterandb _ctx = new Veterandb();
        const int PageSize = 6;
        public ActionResult IndexTag(int page = 1)
        {
            var model = new HomeAdminListViewModel();
            var tags = _ctx.tags.OrderByDescending(p => p.tag_id).ToList();
            model.Tags = tags.Skip((page - 1) * PageSize).Take(PageSize).ToList();
            model.TagPageInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = tags.Count() };
            return View(model);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public ActionResult Paging(int page = 1)
        {
            var model = new HomeAdminListViewModel();
            var tags = _ctx.tags.OrderByDescending(p => p.tag_id).ToList();
            model.Tags = tags.Skip((page - 1) * PageSize).Take(PageSize).ToList();
            model.TagPageInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = tags.Count() };
            return PartialView("UCTagList", model);
        }

        public ActionResult Details(int id)
        {
            // TODO: Add update logic here
            // if (!IsAuthenticateAdmin())
            //   return RedirectToAction("LogOn", "AdminHome");
            var tag = _ctx.tags.SingleOrDefault(c => c.tag_id == id);
            if (tag != null)
            {
                return View(tag);
            }
            else
            {
                tag = new tag();
                return View(tag);
            }
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Details(int id, HttpPostedFileBase uploadFile)
        {
            try
            {
                // TODO: Add update logic here
                // if (!IsAuthenticateAdmin())
                //   return RedirectToAction("LogOn", "AdminHome");
                var tag = _ctx.tags.SingleOrDefault(p => p.tag_id == id);
                UpdateModel(tag);
                _ctx.SaveChanges();
                ViewData["Msg"] = "Đã cập nhật!";
                return View(tag);
            }
            catch
            {
                ViewData["Msg"] = "Đã có lỗi xẩy ra. Vui lòng kiểm tra lại dữ liệu!";
                return View();
            }
        }

        public ActionResult Delete(int id)
        {
            // TODO: Add delete logic here
            //if (!IsAuthenticateAdmin())
            //    return RedirectToAction("LogOn", "AdminHome");
            if (id <= 0)
                return RedirectToAction("Create", "ManageTag", new tag());
            var tag = _ctx.tags.SingleOrDefault(p => p.tag_id == id);
            _ctx.DeleteObject(tag);
            _ctx.SaveChanges();
            return RedirectToAction("IndexTag");
        }

        public ActionResult Create()
        {
            var tag = new tag();
            return View(tag);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create([Bind(Exclude = "tag_id")]tag tag, HttpPostedFileBase uploadFile)
        {
            // TODO: Add update logic here
            // if (!IsAuthenticateAdmin())
            //   return RedirectToAction("LogOn", "AdminHome");
            try
            {
                _ctx.AddTotags(tag);
                _ctx.SaveChanges();
                ViewData["Msg"] = "Đã tạo thành công!";
                return View();
            }
            catch
            {
                ViewData["Msg"] = "Đã có lỗi xẩy ra. Vui lòng kiểm tra lại dữ liệu!";
                return View(string.Empty);
            }
        }
        //check esxist user name
        public bool CheckName(string name)
        {
            try
            {
                tag tg = _ctx.tags.Where(p => p.tag_text.Trim() == name.Trim()).SingleOrDefault() as tag;
                if (tg != null)
                    return false;
                else
                    return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
